﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Write.Applications.Commands.TeamCommands;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Write.Applications.CommandHandlers.TeamCommandHanders
{
    public class UpdTeamCommandHanders : IRequestHandler<UpdTeamCommands, APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public UpdTeamCommandHanders(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(UpdTeamCommands request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var model = mapper.Map<Team>(request);

            model.Del = false;

            result.data = await db.Updateable<Team>(model).ExecuteCommandAsync();

            if (result.data > 0)
            {
                result.code = APIResultCode.success;
                result.msg = "修改交接班信息成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "修改交接班信息失败";
            }

            return await Task.FromResult(result);
        }
    }
}
